package cn.nexd.map.rendering.overlay;

import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.TypedValue;
import android.view.MotionEvent;

import java.util.ArrayList;
import java.util.List;

import cn.nexd.map.rendering.core.SVGMapBaseOverlay;
import cn.nexd.map.rendering.core.componet.SVGMapView;

/**
 * 用于绘制路径信息的图层。
 * Created by codingfish on 15/9/8.
 */
public class SVGMapLinesOverlay extends SVGMapBaseOverlay {

    // Color for compass
    private static final int DEFAULT_LOCATION_COLOR = 0xFF3EBFC9;
    private static final int DEFAULT_LOCATION_SHADOW_COLOR = 0xFF909090;
    private float defaultLocationCircleRadius;


    private Paint locationPaint;
    private SVGMapView svgMapView;
    private List<PointF> positions;


    public SVGMapLinesOverlay(SVGMapView svgMapView) {
        this.svgMapView = svgMapView;
        this.initLayer(svgMapView);
    }


//    public void setPositions(PointF... pointFs) {
//        this.pointFs = pointFs;
//    }

    public void setPositions(List<PointF> positions) {

        List<PointF> pointFs = new ArrayList<>();
        for (int i = 0; i < positions.size(); i++) {
//            PointF pointF = new PointF(positions.get(i).x - svgMapView.getFloorFeature().getOffset_x(), positions.get(i).y - svgMapView.getFloorFeature().getOffset_y());
            PointF pointF = new PointF(positions.get(i).x, positions.get(i).y);
            pointFs.add(pointF);
        }
        this.positions = pointFs;
    }

    private void initLayer(SVGMapView svgMapView) {
        this.showLevel = LOCATION_LEVEL - 1;
        //
        locationPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        locationPaint.setAntiAlias(true);
        locationPaint.setStyle(Paint.Style.FILL);
        locationPaint.setColor(DEFAULT_LOCATION_COLOR);
        locationPaint.setStrokeWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, svgMapView.getResources().getDisplayMetrics()));
        locationPaint.setShadowLayer(5, 3, 3, DEFAULT_LOCATION_SHADOW_COLOR);
        //
        defaultLocationCircleRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, svgMapView.getResources().getDisplayMetrics());
        //

    }

    @Override
    public void onDestroy() {
        positions.clear();
    }

    @Override
    public void onPause() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onTap(MotionEvent event) {

    }


    @Override
    public void draw(Canvas canvas, Matrix matrix, float currentZoom, float currentRotateDegrees) {
        canvas.save();
        if (this.isVisible && this.positions != null && this.positions.size() > 1) {
            float[] startPosition;
            float[] endPosition;
            for (int i = 0, len = positions.size(); i < len - 1; i++) {
                startPosition = new float[]{positions.get(i).x, positions.get(i).y};
                endPosition = new float[]{positions.get(i + 1).x, positions.get(i + 1).y};
                matrix.mapPoints(startPosition);
                matrix.mapPoints(endPosition);
                // 画一个圆
                canvas.drawCircle(startPosition[0], startPosition[1], defaultLocationCircleRadius, locationPaint);
                canvas.drawCircle(endPosition[0], endPosition[1], defaultLocationCircleRadius, locationPaint);

                canvas.drawLine(startPosition[0], startPosition[1], endPosition[0], endPosition[1], locationPaint);
            }
        }

        canvas.restore();
    }
}
